package it.tukano.swingdeco.timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public abstract class SwingTimer {
    
    private final Timer timer;

    /**
     * Instance initializer
     */
    public SwingTimer() {
        timer = new Timer(100, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                SwingTimer.this.doUpdate();
            }
        });
    }

    protected abstract void doUpdate();
    
    public SwingTimer setRepeating(boolean rep) {
        timer.setRepeats(rep);
        return this;
    }
    
    public SwingTimer start() {
        timer.restart();
        return this;
    }
    
    public SwingTimer withDelay(int ms) {
        timer.setInitialDelay(ms);
        timer.setDelay(ms);
        return this;
    }
}
